unit IdeWizard;

interface

uses
  vcl.Menus,
  ToolsAPI;

type
  TIdeWizard = class(TNotifierObject, IOTAWizard)
  private
    mMenu: TMenuItem;
  protected
    procedure mIdeMessage(const aValue: String);
    procedure mMenuCreate();
    procedure mMenuInstall();
  public
    constructor Create();
    destructor Destroy(); override;

    procedure OnMenuClick0(Sender: TObject);

    // IOTAWizard methods
    function GetIDString: String;
    function GetName: String;
    function GetState: TWizardState;
    procedure Execute;
  end;

implementation

uses
  System.SysUtils;

{ TIdeWizard }

procedure TIdeWizard.mIdeMessage(const aValue: String);
var
  fMessageGroup: IOTAMessageGroup;
begin
  with (BorlandIDEServices as IOTAMessageServices) do begin
    fMessageGroup := GetGroup('Package1');
    if fMessageGroup = nil then begin
      fMessageGroup := AddMessageGroup('Package1');
    end;

    AddTitleMessage(aValue, fMessageGroup);
    ShowMessageView(fMessageGroup);
  end;
end;

procedure TIdeWizard.mMenuCreate();
var
  newMenu: array[0..0] of TMenuItem;
  i: Integer;
begin
  mMenu := TMenuItem.Create(nil);
  mMenu.Name := 'Package1Menu';
  mMenu.Caption := 'Package1';

  newMenu[0] := TMenuItem.Create(mMenu);
  newMenu[0].Name := 'Menu1';
  newMenu[0].Caption := 'Menu1';
  newMenu[0].OnClick := Self.OnMenuClick0;

  for i := Low(newMenu) to High(newMenu) do begin
    mMenu.Add(newMenu[i]);
  end;
end;

procedure TIdeWizard.OnMenuClick0(Sender: TObject);
begin
  mIdeMessage('OnMenuClick0');
end;

procedure TIdeWizard.mMenuInstall();
var
  fSvsc: INTAServices;
  fMainMenu: TMainMenu;
  fToolsMenu: TMenuItem;
  I: Integer;
begin
  mMenuCreate();

  if Supports(BorlandIDEServices, INTAServices, fSvsc) then begin
    fMainMenu := fSvsc.MainMenu;

    for I := 0 to fMainMenu.Items.Count -1 do begin
      if AnsiCompareText('ToolsMenu', fMainMenu.Items[i].Name) = 0 then begin
        fToolsMenu := fMainMenu.Items[I];
        fMainMenu.Items.Insert(fMainMenu.Items.IndexOf(fToolsMenu) +1, mMenu);
        Break;
      end;
    end;
  end;
end;

constructor TIdeWizard.Create();
begin
  inherited;

  mMenuInstall();
end;

destructor TIdeWizard.Destroy();
begin
  if Assigned(mMenu) then begin
    while mMenu.Count > 0 do begin
      mMenu[0].Free;
    end;
    FreeAndNil(mMenu);
  end;

  inherited;
end;

procedure TIdeWizard.Execute;
begin
  // do nothing
end;

function TIdeWizard.GetIDString: String;
begin
  Result := 'GetIDString';
end;

function TIdeWizard.GetName: String;
begin
  Result := 'GetName';
end;

function TIdeWizard.GetState: TWizardState;
begin
  Result := [wsEnabled];
end;

end.
